package com.example.gfsquadwebviewproject.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.example.gfsquadwebviewproject.data.setting.SettingConfigData;
import com.example.gfsquadwebviewproject.input.InputPwdNumberAdapter;
import com.example.gfsquadwebviewproject.input.OnInputNumberCodeCallback;
import com.example.gfsquadwebviewproject.input.ScrollGridView;
import com.example.gfsquadwebviewproject.R;
import com.example.gfsquadwebviewproject.util.MyCountDownTimer;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by Administrator on 2016/9/28.
 */
public class MyDialog extends Dialog implements AdapterView.OnItemClickListener, MyCountDownTimer.ICountDownCallback {

    Context context;

    private Stack<Integer> mNumberStack;//保存输入的数字
    private List<TextView> mNumberViewList;//保存密码显示的几个text
    private final static int NUMBER_BUTTON_DELETE = 11; //删除键
    private final static int NUMBER_BUTTON_ZERO = 10;//0号按键
    private final static int NUMBER_BUTTON_CLEAR = 9;//清除按键
    private final static int NUMBER_COUNT = 6;

    //密码框TextView
    private TextView mNumber1TextView;
    private TextView mNumber2TextView;
    private TextView mNumber3TextView;
    private TextView mNumber4TextView;
    private TextView mNumber5TextView;
    private TextView mNumber6TextView;

    TextView  tv_hint_error_password;//提示密码错误

    //限时倒数
    TextView tv_timeTick;

    private ScrollGridView mNumbersGridView; // 输入按键密码gridView
    private InputPwdNumberAdapter mNumberAdapter;    // 数字adapter
    private boolean mIsPassword = true;
    private final static String PASSWORD_NUMBER_SYMBOL = "●";

    private OnInputNumberCodeCallback mCallback; // 返回结果的回调

    MyCountDownTimer myCountDownTimer;////限时控件
    SettingConfigData settingConfigData;//其他设置的配置数据

    public MyDialog(Context context, OnInputNumberCodeCallback mCallback) {
        super(context, R.style.dialog);
        this.context=context;
        this.mCallback = mCallback;
        View mView = LayoutInflater.from(context).inflate(R.layout.intput_dialog, null);

        mNumberStack = new Stack<>();
        mNumberViewList = new ArrayList<>();
        mNumberAdapter = new InputPwdNumberAdapter(context);

        mNumber1TextView = mView.findViewById(R.id.number_1_textView);
        mNumber2TextView = mView.findViewById(R.id.number_2_textView);
        mNumber3TextView = mView.findViewById(R.id.number_3_textView);
        mNumber4TextView = mView.findViewById(R.id.number_4_textView);
        mNumber5TextView = mView.findViewById(R.id.number_5_textView);
        mNumber6TextView = mView.findViewById(R.id.number_6_textView);

        mNumberViewList.add(mNumber1TextView);
        mNumberViewList.add(mNumber2TextView);
        mNumberViewList.add(mNumber3TextView);
        mNumberViewList.add(mNumber4TextView);
        mNumberViewList.add(mNumber5TextView);
        mNumberViewList.add(mNumber6TextView);

        mNumbersGridView = mView.findViewById(R.id.numbers_gridView);
        mNumbersGridView.setAdapter(mNumberAdapter);
        mNumbersGridView.setOnItemClickListener(this);

        tv_timeTick = mView.findViewById(R.id.tv_timeTick);
        tv_hint_error_password = mView.findViewById(R.id.tv_hint_error_password);


        super.setContentView(mView);
        settingConfigData = new SettingConfigData(context);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //限时控件 检测是否触碰
        if (myCountDownTimer==null) {
            myCountDownTimer = new MyCountDownTimer(context,this,settingConfigData.getLogin_view_timeout());
        }
        myCountDownTimer.dispatchTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }


    /**
     * 数字按键的响应时间
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //取消键
        if (position == NUMBER_BUTTON_CLEAR) {
            dismiss();
            clearnNumber();
            return;
        }
        //删除键
        if (position == NUMBER_BUTTON_DELETE) {
            deleteNumber();
        } else {
            if (position == NUMBER_BUTTON_ZERO) {
                mNumberStack.push(0);
            } else {
                mNumberStack.push(++position);
            }
        }
        refreshNumberViews(mNumberStack);
        if (mNumberStack.size() == NUMBER_COUNT) {
            StringBuilder codeBuilder = new StringBuilder();
            for (int number : mNumberStack) {
                codeBuilder.append(number);
            }
            mCallback.onSuccess(codeBuilder.toString());
        }

    }

    /**
     * 清空mNumberStack的内容并刷新密码格
     */
    public void clearnNumber() {
        mNumberStack.clear();
        refreshNumberViews(mNumberStack);
    }

    /**
     * 删除密码位数
     */
    public void deleteNumber() {
        if (mNumberStack.empty() || mNumberStack.size() > NUMBER_COUNT) {
            return;
        }
        mNumberStack.pop();
    }

    public void setTv_hint_error_password(boolean show){
        if (show) {
            tv_hint_error_password.setVisibility(View.VISIBLE);
        }
        else {
            tv_hint_error_password.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 刷新输入框显示
     * @param mNumberStack
     */
    public void refreshNumberViews(Stack<Integer> mNumberStack) {

        for (int i = 0, size = mNumberViewList.size(); i < size; i++) {
            int numSize = mNumberStack.size();
            if (i < numSize) {
                if (mIsPassword) {
                    mNumberViewList.get(i).setText(PASSWORD_NUMBER_SYMBOL);
                } else {
                    mNumberViewList.get(i).setText(String.valueOf(mNumberStack.get(i)));
                }
            } else {
                mNumberViewList.get(i).setText("");
            }
            setTv_hint_error_password(false);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        //显示时启动限时
        if (myCountDownTimer == null) {
            myCountDownTimer = new MyCountDownTimer(context,this,settingConfigData.getLogin_view_timeout());
        }
        myCountDownTimer.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        clearnNumber();
        //停止限时
        if (myCountDownTimer!=null) {
            myCountDownTimer.stop();
            myCountDownTimer = null;
        }
    }

    //限时倒计时
    @Override
    public void onTimeTick(int second) {
        tv_timeTick.setText(""+second);
    }
    //限时 时间结束
    @Override
    public void onTimeOut() {
        dismiss();
    }

}
